<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class UserController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        //房间列表
        return view('home');
    }

    //查询user的信息
    public function user_info()
    {

        //查询user信息
        return view('user.user_info');
    }

    //修改密码
    public function exit_password()
    {

        //查询user信息
        return view('user.exit_password');
    }

    //直播设置
    public function live()
    {
        $user_id = auth()->id();

        //查询是否开通了直播
        $live_data = DB::table('live_room')->where('user_id',$user_id)->first();
        $live_type_list = DB::table('live_room_type')->get();
        if (empty($live_data))
        {
            return view('user.live1',['user_id'=>$user_id]);
        }else{
//            dump(Storage::url($live_data->img));
            return view('user.live2',['live'=>$live_data,'type_list'=>$live_type_list]);
        }
    }

    //开通直播间
    public function createLive(Request $request)
    {
//        dump($request);
        $request->validate([
            'user_id' => 'required|unique:live_room|max:255'
        ]);

        $user_id = $request['user_id'];

        DB::beginTransaction();
        $live_data = DB::table('live_room')->where('user_id', $user_id)->first();
        if (empty($live_data)) {
            $data['title'] = '';
            $data['user_id'] = $user_id;
            $data['flow_name'] = $this->_getRandomString(5);
            $data['create_date'] = date('Y-m-d H:i:s');
            DB::table('live_room')->insert($data);
            DB::commit();
            return redirect("user/live");
        } else {
            DB::rollBack();
        }
        //查询是否存在
    }

    //修改直播间信息
    public function updateLive(Request $request)
    {
        //表单验证
        $validatedData = $request->validate([
            'title' => 'required|max:80|min:4',
            'type_id' => 'required|gt:0',
            'img'=>'dimensions:min_width=50,min_height=100',
        ],[
            'gt' => '直播栏目 必须选择一项',
            'dimensions' => '上传类型必须为图片'
        ],[
            'title' => '标题',
            'type_id' => '直播栏目',
        ]);

        //文件
        $file = $request->file('img');
        if (!empty($file) && $file->isValid())
        {
//            //原文件名
            $OriginalName = $file->getClientOriginalName();
            $ext = $file->getClientOriginalExtension();
            $type = $file->getClientMimeType();
            $realPath = $file->getRealPath();
            $filename = date('Y-m-d-H-i-s').'-'.uniqid().'.'.$ext;
            Storage::disk('uploads')->put($filename,file_get_contents($realPath));
            $data['img'] = 'uploads/'.$filename;
//            //自动改名并保存
//            $path = $request->file('img')->store('uploads');
//            $data['img'] = $path;
        }

        $data['type_id'] = $request->input('type_id',0) ;
        $data['is_play'] = $request->input('is_play',0) ;
        $data['title']   = $request->input('title', auth()->user().'的直播间') ;
        $user_id = $request->input('user_id') ;

        DB::table('live_room')->where('user_id',$user_id)->update($data);
        return redirect("user/live")->with('msg','修改成功');
    }

    //修改直播间的流名称
    public function video_stream_name_update()
    {
        $this->_getRandomString();

    }



    private function _getRandomString($len=5, $chars=null)
    {
        if (is_null($chars)) {
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }

}
